/*根据需求完成代码:
1.定义狗类
属性:年龄，颜色
行为:eat(String something)(something表示吃的东西)
     看家lookHome方法(无参数)
2.定义猫类
属性:年龄，颜色
行为:eat(String something)方法(something表示吃的东西)
     逮老鼠catchMouse方法(无参数)
3.定义Person类//饲养员
属性:姓名，年龄
行为:keepPet(Dog dog,String something)方法
      功能:喂养宠物狗，something表示喂养的东西
行为:keepPet(Cat cat,String something)方法
      功能:喂养宠物猫，something表示喂养的东西
4.定义测试类(完成以下打印效果):
keepPet(Dog dog,String something)方法打印内容如下:
        年龄为30岁的老王养了一只黑颜色的2岁的狗
        2岁的黑颜色的狗两只前腿死死的抱住骨头猛吃
keepPet(Cat cat,String something)方法打印内容如下:
        年龄为25岁的老李养了一只灰颜色的3岁的猫
        3岁的灰颜色的猫眯着眼睛侧着头吃鱼
*/
class Animal{
    private int age;
    private String color;
    public Animal() {}
    public Animal(int age, String color) {
        this.age = age;
        this.color = color;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public void eat(String something){
        System.out.println("年龄为"+age+"岁的"+color+"颜色的动物正在吃"+something);
    }
}
class Dog extends Animal {
    public Dog() {
    }
    public Dog(int age, String color) {
        super(age, color);
    }
    @Override
    public void eat(String something) {
        System.out.println(getAge() + "岁的" + getColor() + "颜色的狗两只前腿死死的抱住" + something + "猛吃");
    }
    public void lookHome() {
        System.out.println("狗在看家");
    }
}
class Cat extends Animal{
    public Cat(){}
    public Cat(int age,String color){
        super(age,color);
    }
    @Override
    public void eat(String something){
        System.out.println(getAge()+"岁的"+getColor()+"颜色的猫眯着眼睛侧着头吃"+something);
    }
    public void catchMouse(){
        System.out.println("猫在逮老鼠");
    }
}
class Person{
    private String name;
    private int age;
    public Person(){}
    public Person(String name,int age){
        this.name=name;
        this.age=age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void keepPet(Dog dog,String something){
        System.out.println("年龄为"+age+"岁的"+name+"养了一只"+dog.getColor()+"颜色的"+dog.getAge()+"岁的狗");
        dog.eat(something);
    }
    public void keepPet(Cat cat,String something){
        System.out.println("年龄为"+age+"岁的"+name+"养了一只"+cat.getColor()+"颜色的"+cat.getAge()+"岁的猫");
        cat.eat(something);
    }
    /*5.思考:
        1.Dog和Cat都是Animal的子类，以上案例中针对不同的动物，定义了不同的Keeppet方法，过于繁琐，能否简化，并体会简化后的好处
        2.Dog和Cat虽然都是Animal的子类，但是都有其特有方法，能否想办法在keepPet中调用特有方法?
    */
    public void keepPet1(Animal animal,String something){
        if(animal instanceof Dog d){
            System.out.println("年龄为"+age+"岁的"+name+"养了一只"+d.getColor()+"颜色的"+d.getAge()+"岁的狗");
            d.eat(something);
            d.lookHome();
        }else if(animal instanceof Cat c){
            System.out.println("年龄为"+age+"岁的"+name+"养了一只"+c.getColor()+"颜色的"+c.getAge()+"岁的猫");
            c.eat(something);
            c.catchMouse();
        }else{
            System.out.println("请输入正确的动物");
        }
    }
}
public class A30 {
    public static void main(String[] args) {
        Person p1=new Person("老王",30);
        Dog d1=new Dog(2,"黑");
        p1.keepPet(d1,"骨头");
        Cat c1=new Cat(3,"灰");
        p1.keepPet(c1,"鱼");
       /*5.思考:
        1.Dog和Cat都是Animal的子类，以上案例中针对不同的动物，定义了不同的Keeppet方法，过于繁琐，能否简化，并体会简化后的好处
        2.Dog和Cat虽然都是Animal的子类，但是都有其特有方法，能否想办法在keepPet中调用特有方法?
       */
        Person p2=new Person("老李",25);
        p2.keepPet1(d1,"骨头");
        p2.keepPet1(c1,"鱼");
    }
}
